package com.vaannila.student;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vaannila.util.HibernateUtil;

public class Main {

	public static void main(String[] args) {
		Student student1 = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			student1 = (Student) session.get(Student.class, 2l);
			
			transaction.commit();
		} catch (HibernateException e) {
			//transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		System.out.println("student1 : 1111 " + student1);
		student1.setStudentName("BBBBB");
		
		
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			transaction = session.beginTransaction();
			//session.refresh(student1);//to reattach	
			//loading this for doing some validation calculation, note that by this time 
			//student1 is not reattached with session, if it was the following get would have returned the 
			//existing 
			Student student2 = (Student) session.get(Student.class, 2l);
			System.out.println("student1 : 2222 " + student2);
			
			Student student3 = (Student) session.merge(student1);
			session.delete(student3);
			transaction.commit();
		} catch (HibernateException e) {
			//transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

}
